package com.example.drugs1.controllers;

import com.example.drugs1.entities.Drugs;
import com.example.drugs1.services.DrugsService;

import com.example.drugs1.services.RoomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.validation.Valid;
import java.util.List;

@Controller
@RequestMapping("/drugs")
public class DrugsControllers {


    @Autowired
    DrugsService drugsService;

    @Autowired
    RoomService roomService;


    @GetMapping
    public String disDrugss(Model model){
        List<Drugs> drugss =drugsService.getALL();
        model.addAttribute("drugss", drugss);
        Authentication authentication=SecurityContextHolder.getContext().getAuthentication();
        if ("anonymousUser".equals(authentication.getName())){
            model.addAttribute("name","未登录");
        }else {
            model.addAttribute("name",authentication.getName());
        }
        return "drugs/list-drugs";
    }

    @GetMapping("/new")
    public String displayRoomFrom(Model model){
        Drugs adrugs=new Drugs();
        model.addAttribute("adrugss",adrugs);
        Authentication authentication=SecurityContextHolder.getContext().getAuthentication();
        System.out.println(authentication.getAuthorities());
        if ("anonymousUser".equals(authentication.getName())){
            model.addAttribute("name","未登录");
        }else {
            model.addAttribute("name",authentication.getName());
        }
        return "drugs/new-drugs";

    }
    @PostMapping("/save")
    public String createdrugs(Model model, @Valid Drugs drugs,@RequestParam("name")String name){
        if (name.equals("")) {
            model.addAttribute("error","注意：名称不能为空");
            return "drugs/new-drugs";
        }else {
            drugsService.save(drugs);
            return "redirect:/drugs";
        }

    }

    @GetMapping("/delete")
    public String deleteDrugs(@RequestParam("id")long theId, Model model){

        Drugs theDrugs = drugsService.findByDugsId(theId);
        drugsService.delete(theDrugs);
        return "redirect:/drugs";
    }

    @RequestMapping("/selects")
    public String selects(Model model){
        Authentication authentication=SecurityContextHolder.getContext().getAuthentication();
        if ("anonymousUser".equals(authentication.getName())){
            model.addAttribute("name","未登录");
        }else {
            model.addAttribute("name",authentication.getName());
        }
        return "drugs/select-drugs";
    }

    @PostMapping("/select")
    public String selectDrugs(@RequestParam("name") String theName,Model model){
        List<Drugs> Sdrugs =drugsService.selectDrugs(theName);
        model.addAttribute("sdrugs", Sdrugs);
        if(Sdrugs.size()<1){
            model.addAttribute("error","查询结果为空，请重新输入");
        }

        return "drugs/select-drugs";
    }


}
